package casess;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-25
 * Time: 21:32
 */
class MyCircularQueue{
    public int[] elem;
    public int front;
    public int rear;

    //空一个格子来判断空满的方案
    public MyCircularQueue(int key) {
        this.elem = new int[key+1];
    }

    //判断满不满
    public boolean isFull(){
        return (rear+1)%elem.length == front;
    }

    //判断空不空
    public boolean isEmpty(){
        return front == rear;
    }

    //入队 判断满不满
    public void put(int val){
        if (isFull()){
            System.out.println("循环队列满了 无法插入");
            return;
        }
        elem[rear] = val;
        rear = (rear+1)%elem.length;
    }

    //出入 判断空不空
    public void poll(){
        if (isEmpty()){
            System.out.println("队列为空！");
            return;
        }
        System.out.println(elem[front]);
        front = (front+1)%elem.length;
    }

}


//实现一个阻塞队列 是一个循环队列 先实现一个循环队列 在实现阻塞
public class Demo5 {
    public static void main(String[] args) {
        //能放7个元素 但是 队列的长度是8 空一个格子来判断空或者满
        MyCircularQueue queue = new MyCircularQueue(7);
        //队列先进先出
        queue.put(1);
        queue.put(12);
        queue.put(23);
        queue.put(34);
        queue.put(45);
        queue.put(56);
        queue.put(67);


        queue.poll();

        queue.put(78);

        queue.poll();
    }
}
